package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.MallMemberFundFlow;
import com.rockcent.mall.lib.domain.MallMemberRechargeFlow;
import com.rockcent.mall.lib.dto.MallMemberFundFlowDto;
import com.rockcent.mall.lib.dto.MallMemberRechargeFlowDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 会员账号资金记录
 * Created by zhangjh on 2017/03/07.
 */
@Repository
public class MallMemberFundFlowRepositoryImpl implements MallMemberFundFlowRepositoryCustom {
    private static final Logger log = LoggerFactory.getLogger(MallMemberRechargeFlowRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    public PageResult<MallMemberFundFlowDto> findMallMemberFundFlowPage(MallMemberFundFlowDto dto, PageResult pageResult) {


        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
//            // a.status,a.name,a.mobile ,b.last_updated ,b.type
            sql.append("select %s from mall_member_fund_flow mm ,mall m where mm.mall_id = m.id ");


            Long mallId = dto.getMallId();

            //序列号
            String seqNo = dto.getSeqNo();

            String beginDate = dto.getBeginDate();

            String endDate = dto.getEndDate();
            //金额
            BigDecimal operationAmount = dto.getOperationAmount();
            String transactionType = dto.getTransactionType();
            String mallName = dto.getMallName();


            if (null != mallId) {
                sql.append(" AND mm.mall_id =:mallId and m.id =:mallId");
                params.put("mallId", mallId);
            }

            if (StringUtils.isNotBlank(mallName)) {
                sql.append(" AND m.name like:mallName");
                params.put("mallName", "%" + mallName + "%");
            }


            if (null != mallId) {
                sql.append(" AND mm.mall_id =:mallId");
                params.put("mallId", mallId);
            }

            if (null != operationAmount) {
                sql.append(" AND mm.operation_amount =:operationAmount");
                params.put("operationAmount", operationAmount);
            }

            if (StringUtils.isNotBlank(seqNo)) {
                sql.append(" AND mm.seq_no =:seqNo");
                params.put("seqNo", seqNo);
            }


            if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
                sql.append(" AND mm.last_updated >=:commitStartDate AND mm.last_updated <:commitEndDate");
                params.put("commitStartDate", RDateUtils.beforeDateStr(beginDate, 0));
                params.put("commitEndDate", RDateUtils.afterDateStr(endDate, 1));

            } else if (StringUtils.isNotBlank(beginDate)) {
                sql.append(" AND mm.last_updated >=:commitStartDate");
                params.put("commitStartDate", RDateUtils.beforeDateStr(beginDate, 0));
            } else if (StringUtils.isNotBlank(endDate)) {
                sql.append(" AND mm.last_updated <:commitEndDate");
                params.put("commitEndDate", RDateUtils.afterDateStr(endDate, 1));
            }

            if (StringUtils.isNotBlank(transactionType)) {
                if (!"ALL".equals(transactionType)) {
                    sql.append(" AND mm.transaction_type =:transactionType");
                    params.put("transactionType", transactionType);
                }
            }
            sql.append(" order by mm.last_updated desc ");

            fields.append(" mm.*,m.name as mallName ");

            log.debug(sql.toString());

        };
        return doing.go(pageResult, MallMemberFundFlowDto.class, objectMapper, jdbcTemplate, log);
    }
}
